package Leetcode第二期;

/**
 * @author : K k
 * @date : 9:53 2020/10/6
 */
public class Pow_50 {

    public double myPow(double x, int n) {
        long N = n;
        return n >= 0 ? quickMul(x, N) : 1.0 / quickMul(x, -N);
    }
    //递归法
    private double quickMul(double x, long N) {
        if (N == 0) {
            return 1.0;
        }
        double y = quickMul(x, N / 2);
        return N % 2 == 0 ? y * y : y * y *x;
    }

    //迭代法
    private double quickMulA(double x, long N) {
       double ans=1.0;
       double x_attribute=x;

       while (N>0){
           if (N%2==1){
               ans *=x_attribute;
           }
           x_attribute *= x_attribute;
           N/=2;
       }
       return ans;
    }
}
